package com.hanrui.android.net.presenter.message.threesupervise;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.constant.LoadType;
import com.hanrui.android.model.ThreeAreaListBean;
import com.hanrui.android.model.ThreeFirmListBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.threesupervise.ThreeSuperviseListContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * Created by menglei on 2018/7/9.
 */

public class ThreeSuperviseListPresenter extends BasePresenter<ThreeSuperviseListContract.View> implements ThreeSuperviseListContract.Presenter {

    private boolean mIsRefresh;

    @Inject
    public ThreeSuperviseListPresenter(){this.mIsRefresh = true;}

    @Override
    public void overSeeThreeFirmList(final Context context, String sourceStatus,String name) {

        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .overSeeThreeFirmList(sourceStatus,name)
                .compose(RxSchedulers.<BaseBean<ThreeFirmListBean>>applySchedulers())
                .compose(mView.<BaseBean<ThreeFirmListBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<ThreeFirmListBean>>() {
                    @Override
                    public void accept(BaseBean<ThreeFirmListBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_SUCCESS :LoadType.TYPE_LOAD_MORE_SUCCESS;
                            mView.overSeeThreeFirmList(dataResponse.getData().getThreeColorList(),loadType);
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                            mView.overSeeThreeFirmList(null,loadType);
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                        mView.overSeeThreeFirmList(null,loadType);
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }

    @Override
    public void overSeeThreeAreaList(final Context context, String sourceStatus,String name) {
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .overSeeThreeAreaList(sourceStatus,name)
                .compose(RxSchedulers.<BaseBean<ThreeAreaListBean>>applySchedulers())
                .compose(mView.<BaseBean<ThreeAreaListBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<ThreeAreaListBean>>() {
                    @Override
                    public void accept(BaseBean<ThreeAreaListBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_SUCCESS :LoadType.TYPE_LOAD_MORE_SUCCESS;
                            mView.overSeeThreeAreaList(dataResponse.getData().getThreeColorList(),loadType);
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                            mView.overSeeThreeAreaList(null,loadType);
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                        mView.overSeeThreeAreaList(null,loadType);
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }

    @Override
    public void refresh(Context context, String sourceStatus,String name) {
        mIsRefresh = true;
        if (sourceStatus.equals("0")) {
            overSeeThreeFirmList(context,sourceStatus,name);
        		}else {
            overSeeThreeAreaList(context,sourceStatus,name);
        }
    }
}
